package cn.xinfei.xdecision.common.service.monitor.logger.impl;

import cn.hutool.core.date.DateUtil;
import cn.xinfei.xdecision.common.dao.monitor.loggermapper.OperateLogMapper;
import cn.xinfei.xdecision.common.model.common.PageResult;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.login.SessionManager;
import cn.xinfei.xdecision.common.model.monitor.logger.OperateLog;
import cn.xinfei.xdecision.common.model.monitor.logger.dto.OperateLogCreateDTO;
import cn.xinfei.xdecision.common.model.monitor.logger.vo.OperateLogPageReqVO;
import cn.xinfei.xdecision.common.model.monitor.logger.vo.OperateLogRespVO;
import cn.xinfei.xdecision.common.service.common.sso.dto.SSOUserInfoRespDto;
import cn.xinfei.xdecision.common.service.common.sso.impl.SSOServiceImpl;
import cn.xinfei.xdecision.common.service.monitor.logger.OperateLogService;
import cn.xinfei.xdecision.common.utils.util.BeanUtilsPlus;
import cn.xinfei.xdecision.common.utils.util.StringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by maying on 2023/04/23.
 **/
@Service
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog> implements OperateLogService {
    @Resource
    private OperateLogMapper operateLogMapper;
    @Resource
    private SSOServiceImpl ssoService;

    /**
     * {@link #javaMethodArgs} 的最大长度
     */
    public static final Integer JAVA_METHOD_ARGS_MAX_LENGTH = 8000;

    /**
     * {@link #resultData} 的最大长度
     */
    public static final Integer RESULT_MAX_LENGTH = 4000;

    @Override
    public void createOperateLog(@Valid OperateLogCreateDTO createReqDTO) {
        OperateLog operateLog = new OperateLog();
        BeanUtils.copyProperties(createReqDTO, operateLog);
        operateLog.setJavaMethodArgs(StringUtil.maxLength(operateLog.getJavaMethodArgs(), JAVA_METHOD_ARGS_MAX_LENGTH));
        operateLog.setResultData(StringUtil.maxLength(operateLog.getResultData(), RESULT_MAX_LENGTH));
        SSOUserInfoRespDto ssoUserInfoRespDto = ssoService.getUserInfoById(createReqDTO.getUserId());
        if (ssoUserInfoRespDto != null) {
            operateLog.setCreateUserName(ssoUserInfoRespDto.getName());
            operateLog.setCreateUserMobile(ssoUserInfoRespDto.getMobile());
        }
        operateLogMapper.insert(operateLog);
    }

    @Override
    public ResponseEntityDto<PageResult<List<OperateLogRespVO>, OperateLog>> getOperateLogPage(OperateLogPageReqVO operateLogPageReqVO) {
        if (StringUtil.isBlank(operateLogPageReqVO.getStartTime())) {
            operateLogPageReqVO.setStartTime(DateUtil.today());
        }
        if (StringUtil.isBlank(operateLogPageReqVO.getEndTime())) {
            operateLogPageReqVO.setEndTime(DateUtil.now());
        }
        PageMethod.startPage(operateLogPageReqVO.getPageNo(), operateLogPageReqVO.getPageSize());
        operateLogPageReqVO.setOrgIdList(SessionManager.getLoginAccount().getOrgIdList());
        List<OperateLog> operateLogList = operateLogMapper.selectPage(operateLogPageReqVO);
        PageInfo<OperateLog> pageInfo = new PageInfo<>(operateLogList);
        List<OperateLogRespVO> operateLogRespVOS = BeanUtilsPlus.copyElements(operateLogList, OperateLogRespVO::new);
        return PageResult.success(operateLogRespVOS, pageInfo);
    }

    @Override
    public OperateLogRespVO getOperateLogInfoById(Long logId) {
        OperateLogRespVO operateLogRespVO = new OperateLogRespVO();
        OperateLog operateLog = this.getById(logId);
        if (operateLog == null || !SessionManager.getLoginAccount().getOrgIdList().contains(operateLog.getOrganId())) {
            return operateLogRespVO;
        }
        BeanUtils.copyProperties(operateLog, operateLogRespVO);
        return operateLogRespVO;
    }
}
